package edu.msg.hrappy.web.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.msg.hrappy.backend.model.Comment;
import edu.msg.hrappy.backend.model.Job;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.service.CommentService;
import edu.msg.hrappy.backend.service.JobService;
import edu.msg.hrappy.backend.service.UserService;

public class DeleteCommentToJobServlet  extends HttpServlet {


		private static final long serialVersionUID = 1L;
		@EJB
	    private JobService jobService;
	    @EJB
	    private UserService userService;
	    @EJB 
	    private CommentService commentService;

	    @Override
	    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
	        req.getSession().setAttribute("er", null);
	    	HttpSession session = req.getSession();
	    	System.out.println("sdkkgh");
			User user = userService.getUserByUserName(req.getUserPrincipal()
					.toString());
			session.setAttribute("userId", user.getId());
			session.setAttribute("username", user.getUserName());
			
			Job job=jobService.getById(Long.parseLong(req.getParameter("jobId")));
			session.setAttribute("job", job);
	        String address = "./OnePozition.jsp";

	        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

	        dispatcher.forward(req, resp);

	    }

	    @Override
	    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
	        
	    	Comment comment = null;
	    	Job job = null;
	    	String jobId = (String) req.getParameter("jobId");
			job = jobService.getById(Long.parseLong(jobId));

			String commentId = (String) req.getParameter("comId");
			comment = commentService.getById(Long.parseLong(commentId));
	      
	     
	        job.removeComment(comment);
	        jobService.update(job);
	        
	        commentService.delete(comment);
	        
	        HttpSession session = req.getSession();
	    	
	        User user = userService.getUserByUserName(req.getUserPrincipal()
					.toString());
			session.setAttribute("userId", user.getId());
			session.setAttribute("username", user.getUserName());
			
			Job job2=jobService.getById(Long.parseLong(req.getParameter("jobId")));
			session.setAttribute("job", job2);
	        String address = "./OnePozition.jsp";

	        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

	        dispatcher.forward(req, resp);

	    }
}
